package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.SysUser;
import com.itheima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

import static com.itheima.constant.MessageConst.DElecT_Role_SUCCESS;

@Controller
@RequestMapping("/user")

@Slf4j
public class UserController {


	@Reference
	UserService userService;

	@RequestMapping("/loginSuccess")
	public String loginSuccess(){
		return "redirect:http://localhost:83/pages/main.html";
	}

	@RequestMapping("/loginFail")
	public String loginFail(){

		return "redirect:http://localhost:83/login.html?flag=true";
	}

	@RequestMapping("/getUsername")
	@ResponseBody
	public Result getUsername(){
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		Object principal = authentication.getPrincipal();
		if(principal instanceof User){
			User user = (User) principal;
			return new Result(true, MessageConst.GET_USERNAME_SUCCESS,user.getUsername());
		}
		return new Result(false, MessageConst.GET_USERNAME_FAIL);
	}

	@RequestMapping("/add")
	@ResponseBody
	public Result add(@RequestBody SysUser sysUser, Integer[]roleIds){
		String password = sysUser.getPassword();
		BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
		String Brcpassword = bCryptPasswordEncoder.encode(password);
		sysUser.setPassword(Brcpassword);
		userService.add(sysUser,roleIds);
		log.debug("用户新增成功!!");
		return new Result(true,MessageConst.ADD_USER_SUCCESS);
	}

	@RequestMapping("/findPage")
	@ResponseBody
	public Result findPage(@RequestBody QueryPageBean queryPageBean){
		log.debug("UserController:findPage:"+queryPageBean);
		PageResult pageResult=userService.findPage(queryPageBean);
		log.debug("用户分页查询成功!!");
		return new Result(true,MessageConst.QUERY_USER_SUCCESS,pageResult);

	}

	@RequestMapping("/delById")
	@ResponseBody
	public Result delById(Integer id){
		log.debug("UserController:delById:{"+id+"}");
		userService.delById(id);
		log.debug("用户删除成功");
		return new Result(true,MessageConst.DElecT_USER_SUCCESS);
	}

	@RequestMapping("/findById")
	@ResponseBody
	public Result findById(Integer id){
		log.debug("UserController:findById:"+id);
		SysUser sysUser= userService.findById(id);
		log.debug("根据id查询用户信息成功!!");
		return new Result(true,"根据id查询用户信息成功!!",sysUser);
	}


	@RequestMapping("/findRoleIdsById")
	@ResponseBody
	public Result findRoleIdsById(Integer id){
		log.debug("UserController:findRoleIdsById:"+id);
		List<Integer> roleIdsById= userService.findRoleIdsById(id);
		log.debug("根据用户id查询角色id成功!!");
		return new Result(true,"根据用户id查询角色id成功!!",roleIdsById);
	}

	@RequestMapping("/edit")
	@ResponseBody
	public Result edit(@RequestBody SysUser sysUser,Integer[]roleIds){
		String password = sysUser.getPassword();
		BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
		String Brcpassword = bCryptPasswordEncoder.encode(password);
		sysUser.setPassword(Brcpassword);
		userService.edit(sysUser,roleIds);
		log.debug("编辑用户成功!!");
		return new Result(true,MessageConst.UPDATE_USER_SUCCESS);
	}
}
